<?php
/**
 * 《我的信息》实用类
 *
 * @author 孙晓晔
 * @version $Id: InfoUtil.class.php 636 2009-11-25 05:49:36Z sunxy $
 */
require_once(dirname(__FILE__) . '/../AdminUtil.class.php');

class ResumeUtil extends AdminUtil {

	public $holiday = array(
		'-' => '请选择',
		'1' => '需要判断',
		'2' => '不需判断',
	);
	
	public $week = array(
		'1' => '星期一',
		'2' => '星期二',
		'3' => '星期三',
		'4' => '星期四',
		'5' => '星期五',
		'6' => '星期六',
		'0' => '星期日',
	);

	public $time = array(
		'-' => '请选择',
		'1' => '上午1',
		'2' => '上午2',
		'3' => '下午1',
		'4' => '下午2',
		'5' => '晚上1',
		'6' => '晚上2',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/03/03 07:24:42 $';
		$request['revision'] = '$Revision: 1.5 $';
			
		parent::__construct($request);
	}	

	/**
	 * 预处理
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('holiday_option', $this->holiday);
		$controller->assign_by_ref('time_option', $this->time);
	}

	function week($date) {
		$w = date('w', strtotime($date));

		return $this->week[$w];
	}

	function _holiday($holiday, $t1, $t2) {
		if (empty($holiday) || $holiday == '2') {
			return false;
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE t1 <= '%s' AND t2 >= '%s'";
		$sql = sprintf($fmt, Constant::tbl_kq_holiday, $t1, $t2);
		debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			return true;
		}

		return false;
	}

	function _date($t1, $t2) {
		$t1 = strtotime($t1);
		$t2 = strtotime($t2);

		$date1 = date("Y-m-d", $t1);
		$date2 = date("Y-m-d", $t2);
		if ($date1 == $date2) {
			return sprintf("%s(%s至%s)", $date1, date("H:i", $t1), date("H:i", $t2));
		}

		return sprintf("%s至%s", date("Y-m-d H:i", $t1), date("Y-m-d H:i", $t2));
	}

	/**
	 * 删除
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}

			$fmt = "DELETE FROM %s WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $id);
			// debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 * 更新状态
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function status($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $status, $id);
		// debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
}